package View;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.wonyesoft.wuliu.R;

/**
 * * @author 作者 陈修园E-mail: * @date 创建时间：2015-6-24 下午9:10:20 * @version 1.0 * @parameter
 * 弹出输入框 * @since * @return
 * */
public class InPutDialog {
	/**
	 * 
	 * @param context
	 * @param 弹出对话框
	 */
	private Dialog alertdialog;

	private Context context;

	private String titleString;

	private String messageString;

	private getData callback;

	/**
	 * 放弃按钮
	 */
	private String buttonGiveup;
	/**
	 * 确定按钮
	 */
	private String buttonCheck;
	
	/**
	 * 图片选择接口
	 */
	private getChoseTYPE imageCallback;

	/**
	 * 用户的点击 事件
	 */
	private getUserIntent userIntentCallback;

	private EditText driverPhone;

	private EditText driverName;

	public InPutDialog(Context context, String titleString, String message,String buttonCheck,String buttonGiveup) {
		this.context = context;
		this.titleString = titleString;
		this.messageString = message;
		this.buttonCheck=buttonCheck;
		this.buttonGiveup=buttonGiveup;
	}

	/**
	 * 弹选择框
	 * 
	 * @param inputDate
	 *          
	 * @return
	 */
	public Dialog getCheckDialog(boolean dismissGiveUP) {
		LinearLayout dateTimeLayout = (LinearLayout) ((Activity) context)
				.getLayoutInflater().inflate(R.layout.alert_dialog, null);
		alertdialog = new AlertDialog.Builder(context).create();
		// alertdialog.set
		alertdialog.show();
		alertdialog.setContentView(dateTimeLayout);
		TextView title = (TextView) dateTimeLayout.findViewById(R.id.title);
		TextView message = (TextView) dateTimeLayout.findViewById(R.id.message);
		title.setText(titleString);
		if (messageString != null) {
			message.setText(messageString);
		}
		Button check = (Button) dateTimeLayout.findViewById(R.id.check);
		if (buttonCheck!=null) {
			check.setText(buttonCheck);
		}
	
		Button giveup = (Button) dateTimeLayout.findViewById(R.id.giveup);
		if (dismissGiveUP) {
			giveup.setVisibility(View.GONE);
		}
		if (buttonGiveup!=null) {
			giveup.setText(buttonGiveup);
		}
	
		check.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				if (userIntentCallback != null) {
					userIntentCallback.getUserIntentInfo();
					alertdialog.dismiss();
				}
				alertdialog.dismiss();
			}
		});
		giveup.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// inputDate.setText("点击选择时间");
				alertdialog.dismiss();
			}
		});
		return alertdialog;
	}

	/**
	 * 弹出输入方法
	 * 
	 * @param inputDate
	 *            :
	 * @return
	 */
	public Dialog getDialog() {
		LinearLayout dateTimeLayout = (LinearLayout) ((Activity) context)
				.getLayoutInflater().inflate(R.layout.input_dialog_view, null);
		alertdialog = new AlertDialog.Builder(context).create();
		// alertdialog.set
		alertdialog.show();
		alertdialog.setContentView(dateTimeLayout);
		TextView title = (TextView) dateTimeLayout.findViewById(R.id.tit);
		title.setText(titleString);
		Button check = (Button) dateTimeLayout.findViewById(R.id.sure);
		Button giveup = (Button) dateTimeLayout.findViewById(R.id.close);
		driverPhone = (EditText) dateTimeLayout.findViewById(R.id.driverPhone);
		driverName = (EditText) dateTimeLayout.findViewById(R.id.driverName);
		check.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String p = driverPhone.getText().toString();
				String n = driverName.getText().toString();
				if (p.length() > 0) {
					if (n.length() > 0) {
						if (callback != null) {
							callback.getDriverInfo(p, n);
							alertdialog.dismiss();
						}
					} else {
						Toast.makeText(context, "请输入司机名字", Toast.LENGTH_LONG)
								.show();
					}
				} else {
					Toast.makeText(context, "请输入司机电话号码", Toast.LENGTH_LONG)
							.show();
				}

			}
		});
		giveup.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// inputDate.setText("点击选择时间");
				alertdialog.dismiss();
			}
		});
		alertdialog.getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
						| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
		alertdialog.getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		return alertdialog;

	}

	
	
	/**
	 * 弹出选择照片的方法
	 * 
	 * @param inputDate
	 *            :
	 * @return
	 */
	public Dialog getPhotoDialog(String chose_1,String chose_2) {
		LinearLayout dateTimeLayout = (LinearLayout) ((Activity) context)
				.getLayoutInflater()
				.inflate(R.layout.choser_photo_dialog, null);
		alertdialog = new AlertDialog.Builder(context).create();
		// alertdialog.set
		alertdialog.show();
		alertdialog.setContentView(dateTimeLayout);
		TextView title = (TextView) dateTimeLayout.findViewById(R.id.tit);
		title.setText(titleString);
		TextView Gallery = (TextView) dateTimeLayout.findViewById(R.id.Gallery);
		if (chose_1!=null) {
			Gallery.setText(chose_1);
		}
		TextView camera = (TextView) dateTimeLayout.findViewById(R.id.camera);
		if (chose_2!=null) {
			camera.setText(chose_2);
		}
		Button giveup = (Button) dateTimeLayout.findViewById(R.id.close);
		driverPhone = (EditText) dateTimeLayout.findViewById(R.id.driverPhone);
		driverName = (EditText) dateTimeLayout.findViewById(R.id.driverName);
		Gallery.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (imageCallback != null) {
					imageCallback.getImageTYPE(1);
				}
				alertdialog.dismiss();
			}
		});

		camera.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (imageCallback != null) {
					imageCallback.getImageTYPE(2);
				}
				alertdialog.dismiss();
			}
		});
		giveup.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				// inputDate.setText("点击选择时间");
				alertdialog.dismiss();
			}
		});
		alertdialog.getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
						| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
		alertdialog.getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		return alertdialog;

	}

	// 显示虚拟键盘
	public static void ShowKeyboard(View v) {
		InputMethodManager imm = (InputMethodManager) v.getContext()
				.getSystemService(Context.INPUT_METHOD_SERVICE);

		imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);

	}

	public void showKeyboard(EditText editText) {
		if (editText != null) {
			// 设置可获得焦点
			editText.setFocusable(true);
			editText.setFocusableInTouchMode(true);
			// 请求获得焦点
			editText.requestFocus();
			// 调用系统输入法
			InputMethodManager inputManager = (InputMethodManager) editText
					.getContext()
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			inputManager.showSoftInput(editText, 0);
			Log.v("应该出来", "应该出来");
		} else {
			Log.v("还是为空", "还是为空");
		}
	}

	/**
	 * 用户图片选择
	 * 
	 * @author luck
	 * 
	 */
	public interface getChoseTYPE {
		public void getImageTYPE(int type);
	}

	public void setGetChoseTYPE(getChoseTYPE photoChoseTYPE) {
		this.imageCallback = photoChoseTYPE;
	}

	public void setGetDataLister(getData callback) {
		this.callback = callback;
	}

	public void setGetUserIntent(getUserIntent userIntentCallback) {
		this.userIntentCallback = userIntentCallback;
	}

	public interface getData {
		public void getDriverInfo(String driverPhone, String driverName);
	}

	public interface getUserIntent {
		public void getUserIntentInfo();
	}

}
